package controllers

import (
	"context"
	"net/http"

	"github.com/gin-gonic/gin"
	"github.com/minio/minio-go/v7"
	"jjqzplucky.site/sz-core/pkg/constants"
	"jjqzplucky.site/sz-core/pkg/minio_client"
	"jjqzplucky.site/sz-core/pkg/utils"
)

type UploadController struct{}

func (u *UploadController) UploadImage(c *gin.Context) {
	client := minio_client.MinioClient
	file, err := c.FormFile("file")
	if err != nil {
		c.Error(err)
	}
	src, err := file.Open()
	if err != nil {
		c.Error(err)
	}
	// 计算文件的哈希值
	hash := utils.CalculateFileHash(src)
	objectName := hash + "." + utils.GetFileExtension(file.Filename)
	bucketName := "hospital"
	_, err = client.PutObject(context.Background(), bucketName, "/doctor/"+objectName, src, -1, minio.PutObjectOptions{ContentType: file.Header.Get("Content-Type")})
	if err != nil {
		c.Status(http.StatusInternalServerError)
		c.Error(err)
		return
	}
	c.Set(constants.ResultData, bucketName+"/"+objectName)
}
